package com.ssmTest.utils;

import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.UUID;


@Service("toolUtil")
public class ToolUtil {


  public static String getDate() {

    return getDate(new Date());
  }

  public static String getDate(Date data) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(data);
  }

  public static String formatDate(String pattern, String src) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    Date date = sdf.parse(src);
    return getDate(date);
  }


  /**
   * 获取配置文件里的配置
   * String property  配置文件名称
   * String name      需要获取的配置参数名称
   * return String 配置参数
   * ToolUtil.getPropert("filepath.properties",user+".sbhg")
   **/
  public static String getPropert(String property, String name) {
    String url = "";
    try {
      ClassLoader cls = Thread.currentThread().getContextClassLoader();
      InputStream in = cls.getResourceAsStream(property);
      Properties p = new Properties();

      p.load(in);
      if (p.containsKey(name)) {
        url = p.getProperty(name);
      }
    } catch (IOException e) {
    }

    return url;
  }

  /**
   * 两个时间差值
   *
   * @time1
   * @time2
   */
  public static Long timedif(String time1, String time2) throws Exception {

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:sss");
    Date now = df.parse(time1);
    Date date = df.parse(time2);
    long l = now.getTime() - date.getTime();

    return l;
  }

  /**
   * 自动生成32位的UUid，对应数据库的主键id进行插入用。
   *
   * @return
   */
  public static String getUUID() {

    return UUID.randomUUID().toString().replace("-", "");
  }

  public void main(String[] args) {

  }


  public static void createFile(String fileUrl) {
    File file = new File(fileUrl);
    if (!file.exists() && !file.isDirectory()) {
      file.mkdirs();
    }
  }

}
